package com.pzr.soccer.service;

import com.pzr.soccer.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserDetailsServiceMongoDBImpl implements UserDetailsService {

	@Autowired
	MongoOperations operations;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		System.out.println("开始登录...");
		User user=operations.findOne(Query.query(Criteria.where("username").is(username)), User.class);
		if(user==null) {
			throw new UsernameNotFoundException("no such user! "+username);
		}
		System.out.println("user 登录："+user.getUsername());
		return user;
	}

}
